package com.uit.project.bustrip.ultil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONUltil {
	public static JSONObject getJSONfromURL(String url){
		InputStream is = null;
		String result = "";
		JSONObject jArray = null;
		
		//http post
	    try{
	            HttpClient httpclient = new DefaultHttpClient();
	            HttpPost httppost = new HttpPost(url);
	            HttpResponse response = httpclient.execute(httppost);
	            HttpEntity entity = response.getEntity();
	            is = entity.getContent();

	    }catch(Exception e){
	            Log.e("log_tag", "Error in http connection "+e.toString());
	    }
	    
	  //convert response to string
	    try{
	            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"),8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                    sb.append(line + "\n");
	            }
	            is.close();
	            result=sb.toString();
	    }catch(Exception e){
	            Log.e("log_tag", "Error converting result "+e.toString());
	    }
	    
	    try{
	    	
            jArray = new JSONObject(result);            
	    }catch(JSONException e){
	            Log.e("log_tag", "Error parsing data "+e.toString());
	    }
    
	    return jArray;
	}
	public static JSONObject getJSONObjectFromURL(String url){
		InputStream is = null;
		JSONObject result = null;
		is = getInputStreamFromURL(url);
		if(is !=  null){
			String rs = getJSONStringFromInputStream(is);
			if(rs != null){
				result = getJSONObjectFromString(rs);
			}
		}else{
			return null;
		}
		return result;
	}
	public static JSONObject getJSONObjectFromString(String jsonString){
		JSONObject jArray = null;
		try{
            jArray = new JSONObject(jsonString);            
	    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
            jArray = null;
	    }
		return jArray;
	}
	public static String getJSONStringFromInputStream(InputStream is){
		String result = "";
		try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
            return result;
	    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
	    }
		return null;
	}
	public static InputStream getInputStreamFromURL (String url){
		InputStream is = null;
		//http post
	    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            return is;
	    }catch(Exception e){
	    	Log.e("log_tag", "Error in http connection "+e.toString());
	    }
	    return null;
	}
}
